package com.lnking.lnkingplace.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lnking.lnkingplace.Util.StringUtil;
import com.lnking.lnkingplace.model.dto.fourEnterprise.FourEnterpriseImport;
import com.lnking.lnkingplace.model.dto.garden.GardenImport;
import com.lnking.lnkingplace.model.entity.FourEnterprise;
import com.lnking.lnkingplace.model.entity.Garden;
import com.lnking.lnkingplace.mapper.GardenMapper;
import com.lnking.lnkingplace.model.request.garden.GardenAddRequest;
import com.lnking.lnkingplace.model.request.garden.GardenUpdateRequest;
import com.lnking.lnkingplace.service.IGardenService;
import com.lnking.starter.export.utils.ImportOption;
import com.lnking.starter.export.utils.ImportUtils;
import com.lnking.starter.mybatis.service.impl.CustomServiceImpl;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangqt
 * @since 2022-10-03
 */
@Service
public class GardenServiceImpl extends CustomServiceImpl<GardenMapper, Garden> implements IGardenService {

    @Override
    public void add(GardenAddRequest params) {
        Garden info = new Garden();
        BeanUtils.copyProperties(params,info);
        info.setCreateTime(LocalDateTime.now());
        this.baseMapper.insert(info);
    }

    @Override
    public void update(GardenUpdateRequest params) {
        Garden info = new Garden();
        BeanUtils.copyProperties(params,info);
        this.baseMapper.updateById(info);
    }

    @Override
    public void importExcel(MultipartFile file) throws Exception {
        ImportOption option = new ImportOption.Builder().build();
        List<GardenImport> result =  ImportUtils.importExcel(file, GardenImport.class,option);
        for (GardenImport gardenImport : result) {
            if(StringUtils.isNotBlank(gardenImport.getName())){
                Garden info = new Garden();
                BeanUtils.copyProperties(gardenImport,info);
                info.setCreateTime(LocalDateTime.now());
                if(StringUtils.isNotBlank(gardenImport.getCoveredArea())) {
                    info.setCoveredArea(Float.valueOf(gardenImport.getCoveredArea()));
                }
                if(StringUtils.isNotBlank(gardenImport.getCulturalCreationTaxation())) {
                    info.setCulturalCreationTaxation(Float.valueOf(gardenImport.getCulturalCreationTaxation()));
                }
                if(StringUtils.isNotBlank(gardenImport.getCulturalCreationTotal())) {
                info.setCulturalCreationTotal(Float.valueOf(gardenImport.getCulturalCreationTotal()));
                }
                if(StringUtils.isNotBlank(gardenImport.getFloorArea())) {
                info.setFloorArea(Float.valueOf(gardenImport.getFloorArea()));
                }
                if(StringUtils.isNotBlank(gardenImport.getOccupancyRate())) {
                info.setOccupancyRate(Float.valueOf(gardenImport.getOccupancyRate()));
                }
                if(StringUtils.isNotBlank(gardenImport.getTaxation())) {
                info.setTaxation(Float.valueOf(gardenImport.getTaxation()));
                }
                if(StringUtils.isNotBlank(gardenImport.getTotalValue())) {
                info.setTotalValue(Float.valueOf(gardenImport.getTotalValue()));
                }

                if(StringUtils.isNotBlank(gardenImport.getCulturalCreationEnterprise())) {
                info.setCulturalCreationEnterprise(Integer.valueOf(gardenImport.getCulturalCreationEnterprise()));
                }
                if(StringUtils.isNotBlank(gardenImport.getEnterpriseTotal())) {
                info.setEnterpriseTotal(Integer.valueOf(gardenImport.getEnterpriseTotal()));
                }
                if(StringUtils.isNotBlank(gardenImport.getIntellectualProperty())) {
                info.setIntellectualProperty(Integer.valueOf(gardenImport.getIntellectualProperty()));
                }
                if(StringUtils.isNotBlank(gardenImport.getRegulatedCulturalCreationEnterprise())) {
                info.setRegulatedCulturalCreationEnterprise(Integer.valueOf(gardenImport.getRegulatedCulturalCreationEnterprise()));
                }
                if(StringUtils.isNotBlank(gardenImport.getRegulatedEnterprises())) {
                info.setRegulatedEnterprises(Integer.valueOf(gardenImport.getRegulatedEnterprises()));
                }
                if(StringUtils.isNotBlank(gardenImport.getStartTime())) {
                    info.setStartTime(HSSFDateUtil.getJavaDate(Double.parseDouble(gardenImport.getStartTime())).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
                }
                this.baseMapper.insert(info);
            }

        }
    }
}
